package cn.iocoder.yudao.module.erp.service.finance;

import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.erp.controller.admin.finance.vo.category.PRCategorySaveVO;
import cn.iocoder.yudao.module.erp.controller.admin.finance.vo.category.PRCategoryVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.finance.ErpFinancePaymentCategoryDO;
import cn.iocoder.yudao.module.erp.dal.mysql.finance.ErpFinancePaymentCategoryMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Validated
public class ErpFinancePaymentCategoryServiceImpl implements ErpFinancePaymentCategoryService {
    @Resource
    private ErpFinancePaymentCategoryMapper erpFinancePaymentCategoryMapper;

    @Resource
    private CashFlowService cashFlowService;
    /**
     * 创建收入类别
     *
     * @param prCategorySaveVO 创建信息
     */
    @Override
    public void createFinancePaymentCategory(PRCategorySaveVO prCategorySaveVO) {
        ErpFinancePaymentCategoryDO category = BeanUtils.toBean(prCategorySaveVO, ErpFinancePaymentCategoryDO.class);
        erpFinancePaymentCategoryMapper.insert(category);
    }

    @Override
    public List<PRCategoryVO> getFinancePaymentCategories() {
        // 1. 查询未删除的当前租户的付款类别
        List<ErpFinancePaymentCategoryDO> categories = erpFinancePaymentCategoryMapper.selectList();

        // 2. 按父类别ID分组，方便后续查找子类别
        Map<Long, List<ErpFinancePaymentCategoryDO>> categoryMap = categories.stream()
                .collect(Collectors.groupingBy(ErpFinancePaymentCategoryDO::getParent));

        // 3. 转换为PRCategoryVO并设置现金流名称和子类别
        return categories.stream()
                .filter(category -> category.getParent() == 1) // 只处理顶级类别
                .map(category -> {
                    PRCategoryVO vo = convertToVO(category);
                    // 设置子类别
                    setChildren(vo, categoryMap);
                    return vo;
                })
                .collect(Collectors.toList());
    }

    // 将DO转换为VO
    private PRCategoryVO convertToVO(ErpFinancePaymentCategoryDO category) {
        PRCategoryVO vo = new PRCategoryVO();
        vo.setId(category.getId());
        vo.setName(category.getName());
        vo.setParent(category.getParent());
        // 仅当有关联现金流时查询名称
        if (category.getCashFlow() != null) {
            vo.setCashFlow(cashFlowService.getCashFlowByID(category.getCashFlow()));
        }
        return vo;
    }

    // 递归设置子类别
    private void setChildren(PRCategoryVO parentVo, Map<Long, List<ErpFinancePaymentCategoryDO>> categoryMap) {
        List<ErpFinancePaymentCategoryDO> children = categoryMap.get(parentVo.getId());
        if (children != null && !children.isEmpty()) {
            parentVo.setChildren(children.stream()
                    .map(child -> {
                        PRCategoryVO.ChildCategory childVo = new PRCategoryVO.ChildCategory();
                        childVo.setId(child.getId());
                        childVo.setName(child.getName());
                        if (child.getCashFlow() != null) {
                            childVo.setCashFlow(cashFlowService.getCashFlowByID(child.getCashFlow()));
                        }
                        return childVo;
                    })
                    .collect(Collectors.toList()));
        }
    }


}
